package com.handlers;

import com.MiniResultSetHanlder;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MiniScalarHandler<T> implements MiniResultSetHanlder<T> {
    private int columnIndex = 1;
    private String columnName;

    public MiniScalarHandler() {
        this(1,null);
    }

    public MiniScalarHandler(int columnIndex) {
        this(columnIndex,null);
    }

    public MiniScalarHandler(String columnName) {
        this(-1,columnName);
    }

    public MiniScalarHandler(int columnIndex, String columnName) {
        this.columnIndex = columnIndex;
        this.columnName = columnName;
    }

    @Override
    public T handle(ResultSet rs) throws SQLException {
        if(rs.next()){
            if(columnName!=null){
                return (T) rs.getObject(columnName);
            }
            return (T) rs.getObject(columnIndex);
        }
        return null;
    }
}
